const req = require('express/lib/request')
const res = require('express/lib/response')
const db = require('../db/index')

// 定义补零的函数
const padZero = (n) => {
    return n > 9 ? n : '0' + n
}

const dataFormat = (date) => {
    const dt = new Date(date);

    var y = dt.getFullYear();
    var m = padZero(dt.getMonth() + 1);
    var d = padZero(dt.getDate());

    var hh = padZero(dt.getHours());
    var mm = padZero(dt.getMinutes());
    var ss = padZero(dt.getSeconds());

    return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
}

// 获取文章
exports.getArticleContent = (req, res, next) => {
    const sql = 'select * from articles where articleId = ?'
    db.query(sql, req.params.id, (err, result) => {
        if(err) return res.show(err)
        // console.log(result)
        req.article = {
          ...result[0],
          pubdate: dataFormat(result[0].pubdate),
        };
        // console.log(req.article);
        // res.render('pages/content', { data: result })
        next()
    })
}

// 获取文章作者
exports.getArticleAuthor = (req, res, next) => {
    // console.log(req.article[0].authorId);
    const sql = 'select * from users where userId = ?'
    db.query(sql, req.article.authorId, (err, result) => {
      if (err) return res.show(err);
    //   console.log(result);
    //   res.render("pages/content", { article: req.article, author: result });
        req.art_author = {
          userId: result[0].userId,
          email: result[0].email,
          username: result[0].username,
          user_pic: result[0].user_pic,
          signature: result[0].signature
        };
        next()
    });
}

// 获取文章评论数据
exports.getCommentByArticleId = (req, res, next) => {
    const sql = 'select * from comments where articleId = ?'
    db.query(sql, req.params.id, (err, result) => {
        if(err) res.show(err)
        // console.log(result)
        req.comments = result
        // res.render("pages/content", {
        //     article: req.article,
        //     author: req.art_author,
        //     comments: result
        // });
        next()
    })
}


// 获取评论的作者
exports.getAllCommentAuthorByArtId = (req, res) => {
    // console.log(req)
    req.allComment = []
    let comments = req.comments
    // console.log(comments)
    if (comments.length > 0) {
      comments.forEach((comment) => {
        const sql = `select * from users where userId = ?`
        db.query(sql, comment.userId, (err, result) => {
          if (err) res.show(err);

          let commentInfo = {
            commAuthor: {
              userId: result[0].userId,
              username: result[0].username,
              user_pic: result[0].user_pic,
            },
            comment: {
              commentId: comment.cmtId,
              content: comment.content,
              approve: comment.approve,
            }
          }
          req.allComment.push(commentInfo)
          if(req.allComment.length === comments.length) {
            // console.log(req.allComment)
            res.render("pages/content", {
              article: req.article,
              author: req.art_author,
              allComment: req.allComment,
            });
          }
        })
      })
    } else {
      // console.log(req.article);
      res.render("pages/content", {
        article: req.article,
        author: req.art_author,
        allComment: req.allComment,
      });
    }
}
